package cn.broadmax.boot.modules.sys.bean.log;

import cn.broadmax.boot.modules.sys.entity.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import jexx.util.StringUtil;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

@Data
@ApiModel(value="日志分页查询请求", description="日志分页查询")
public class SysLogPageSearch {

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "开始时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startLogTime;

    @ApiModelProperty(value = "结束时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endLogTime;

    public static LambdaQueryWrapper<SysLog> createWrap(SysLogPageSearch search){
        LambdaQueryWrapper<SysLog> wrapper = new LambdaQueryWrapper<>();
        if(search != null){
            if(StringUtil.hasText(search.getUsername())){
                wrapper.like(SysLog::getUsername, search.getUsername());
            }
            if(search.getStartLogTime() != null && search.getEndLogTime() != null){
                wrapper.between(SysLog::getLogTime, search.getStartLogTime(), search.getEndLogTime());
            }
            else if(search.getStartLogTime() != null ){
                wrapper.ge(SysLog::getLogTime, search.getStartLogTime());
            }
            else if(search.getEndLogTime() != null){
                wrapper.le(SysLog::getLogTime, search.getEndLogTime());
            }
        }
        wrapper.orderByDesc(SysLog::getLogTime);
        return wrapper;
    }

}
